package com.cdks.wuxiu.api.service;


import org.springframework.stereotype.Service;

import com.cdks.wuxiu.api.component.cdks.kmsg.SingleMessage;
import com.cdks.wuxiu.api.component.constant.Constant;
import com.cdks.wuxiu.api.service.mysql.CallSheetInfoService;
import com.cdks.wuxiu.api.service.mysql.ChatMessageAuditInfoService;
import com.cdks.wuxiu.api.service.mysql.UserBaseInfoService;
import com.cdks.wuxiu.api.service.mysql.UserBillInfoService;
import com.cdks.wuxiu.api.service.mysql.UserIdiomInfoService;
import com.cdks.wuxiu.api.service.mysql.UserLoginInfoService;
import com.cdks.wuxiu.api.service.mysql.UserScoreInfoService;
import com.cdks.wuxiu.api.service.mysql.UserWalletInfoService;
import com.cdks.wuxiu.api.service.mysql.ChatMessageInfoService;
import com.cdks.wuxiu.api.service.redis.RedisService;
import com.cdks.wuxiu.api.util.TimeUtils;
import com.cdks.wuxiu.entity.message.ChatPrivateData;
import com.cdks.wuxiu.entity.mysql.UserIdiomInfo;

import lombok.extern.slf4j.Slf4j;

import java.util.List;

import javax.annotation.Resource;


/**
 * 消息业务类
 */
@Slf4j
@Service
public class MessageServiceImpl implements MessageService {
    @Resource
    private ChatMessageInfoService chatMessageInfoService;
    @Resource
    private ChatMessageAuditInfoService chatMessageAuditInfoService;
    @Resource
    private UserIdiomInfoService userIdiomInfoService;    
    @Resource
    private UserBaseInfoService userBaseInfoService;
    @Resource
    private UserLoginInfoService userLoginInfoService;
    @Resource
    private CallSheetInfoService callSheetInfoService;  
    @Resource
    private UserScoreInfoService userScoreInfoService; 
    @Resource
    private UserWalletInfoService userWalletInfoService;
    @Resource
    private UserBillInfoService userBillInfoService;    
    @Resource
    private RedisService redisService;


    @Override
    public int saveUserMessage(long senderId, long recverId, int type, String data) {
        //保存消息记录
        int ret  = chatMessageInfoService.saveMessage(senderId, recverId, type, data);
        if (ret < 0)
        {
            return -1;
        }

        return 0;
    }

    @Override
    public int saveAuditMessage(long senderId, long recverId, int type, String data, String remark) {
        return chatMessageAuditInfoService.saveMessage(senderId, recverId, type, data, remark);
    }

    @Override
    public int sendUserMessage(long senderId, long recverId, int type, ChatPrivateData data, boolean push) {
        String umid  = TimeUtils.getLocalDateTime("yyyyMMddHHmmssSSS");
        return sendUserMessage(senderId, recverId, type, data, umid, push);
    }

    @Override
    public int sendUserMessage(long senderId, long recverId, int type, ChatPrivateData data, String umid, boolean push) {
        //发送消息
        SingleMessage.sendChatMessage(senderId, recverId, type, Constant.ENABLE_NO, data, umid);
        //离线消息推送
        if (push)
        {
            switch(type)
            {
                case Constant.MSG_TYPE_TEXT:
                case Constant.MSG_TYPE_LINK:
                {
                    sendOfflineNotice(recverId, "新消息提醒", data.getContent());

                    break;
                }
                case Constant.MSG_TYPE_IMAGE:
                {
                    sendOfflineNotice(recverId, "新消息提醒", "您收到一条[图片消息]");

                    break;
                }
                case Constant.MSG_TYPE_VOICE:
                {
                    sendOfflineNotice(recverId, "新消息提醒", "您收到一条[语音消息]");

                    break;
                }
                case Constant.MSG_TYPE_BOOK:
                {
                    sendOfflineNotice(recverId, "新消息提醒", "您有一笔预约咨询订单待确认");

                    break;
                }
                default:
                {
                    sendOfflineNotice(recverId, "温馨提示", "您收到一条新消息");
                }                                
            }
        }

        return 0;
    }

    @Override
    public int sendUserMessage(long senderId, long[] recverId, int type, ChatPrivateData data, boolean push) {
        //发送消息
        SingleMessage.sendChatMessage(senderId, recverId, type, Constant.ENABLE_NO, data);
        //离线消息推送
        if (push)
        {
            switch(type)
            {
                case Constant.MSG_TYPE_TEXT:
                case Constant.MSG_TYPE_LINK:
                {
                    sendOfflineNotice(recverId, "新消息提醒", data.getContent());

                    break;
                }
                case Constant.MSG_TYPE_IMAGE:
                {
                    sendOfflineNotice(recverId, "新消息提醒", "您收到一条[图片消息]");

                    break;
                }
                case Constant.MSG_TYPE_VOICE:
                {
                    sendOfflineNotice(recverId, "新消息提醒", "您收到一条[语音消息]");

                    break;
                }
                case Constant.MSG_TYPE_BOOK:
                {
                    sendOfflineNotice(recverId, "新消息提醒", "您有一笔预约咨询订单待确认");

                    break;
                }                
                default:
                {
                    sendOfflineNotice(recverId, "温馨提示", "您收到一条新消息");
                }                                
            }
        }

        return 0;
    }
    
    @Override
    public int sendCallMessage(long senderId, long recverId, int type, Object data) {
        //发送消息
        SingleMessage.sendCallMessage(senderId, recverId, type, Constant.ENABLE_NO, data);

        return 0;
    }
    
    @Override
    public int sendCallMessage(long senderId, long[] recverId, int type, Object data) {
        //发送消息
        SingleMessage.sendCallMessage(senderId, recverId, type, Constant.ENABLE_NO, data);

        return 0;
    }

    @Override
    public int sendSystemMessage(long []recverId, int type, Object data) {
        //发送消息
        SingleMessage.sendChatMessage(Constant.USERID_SYSTEM, recverId, type, Constant.ENABLE_NO, data);

        return 0;
    }

    @Override
    public int sendSystemMessage(long recverId, int type, Object data) {
        //发送消息
        SingleMessage.sendChatMessage(Constant.USERID_SYSTEM, recverId, type, Constant.ENABLE_NO, data);

        return 0;
    }
    
    @Override
    public int sendOfflineNotice(long []userId, String title, String content) {
        SingleMessage.sendOfflineNotice(userId, title, content); 

        return 0;
    }
    
    @Override
    public int sendOfflineNotice(long userId, String title, String content) {
        SingleMessage.sendOfflineNotice(userId, title, content); 

        return 0;
    }
    
    @Override
    public int sendOfflineNotice(long[] userId, String title, String content, String sound) {
        SingleMessage.sendOfflineNotice(userId, title, content, sound); 

        return 0;
    }
    
    @Override
    public int sendOfflineNotice(long userId, String title, String content, String sound) {
        SingleMessage.sendOfflineNotice(userId, title, content, sound); 

        return 0;
    }

    @Override
    public int saveRepeatMessage(long senderId, long recverId, int type, long ts, String data, String md5) {
        redisService.saveRepeatMessage(senderId, md5, data);

        return 0;
    }

    @Override
    public int setLastMessageMd5(long userId, String md5) {
        redisService.setLastMessageMd5(userId, md5);

        return 0;
    }

    @Override
    public String getLastMessageMd5(long userId) {
        return redisService.getLastMessageMd5(userId);
    }

    @Override
    public int getMessageRate(long senderId, long recverId) {
        return redisService.getMessageRate(senderId, recverId);
    }

    @Override
    public int delMessageRate(long senderId, long recverId) {
        return redisService.delMessageRate(senderId, recverId);
    }

    @Override
    public int ttlMessageRate(long senderId, long recverId) {
        return redisService.ttlMessageRate(senderId, recverId);
    }

    @Override
    public int addMessageRate(long senderId, long recverId) {
        return redisService.addMessageRate(senderId, recverId);
    }

    @Override
    public int decMessageRate(long senderId, long recverId) {
        return redisService.decMessageRate(senderId, recverId);
    }

    @Override
    public int addMessageCount(long senderId, long recverId) {
        return redisService.addMessageCount(senderId, recverId);
    }

    @Override
    public int ttlChatBanTime(long senderId) {
        return redisService.ttlChatBanTime(senderId);
    }

    @Override
    public int addChatBanCount(long senderId) {
        return redisService.addChatBanCount(senderId);
    }

    @Override
    public int addIdiom(long userId, String content) {
        return userIdiomInfoService.addIdiom(userId, content);
    }

    @Override
    public int editIdiom(long userId, long id, String content) {
        return userIdiomInfoService.editIdiom(userId, id, content);
    }

    @Override
    public int delIdiom(long userId, long id) {
        return userIdiomInfoService.delIdiom(userId, id);
    }

    @Override
    public List<UserIdiomInfo> getIdiomList(long userId, int page) {
        return userIdiomInfoService.getList(userId, page);
    }
}
